package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.EnterpriseList;
import com.isunland.managesystem.entity.OrderCollectContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

public class SearchOrderSaleCollectFragment extends Fragment implements OnClickListener {
  protected static final String EXTRA_VLAUE =
      "com.isunland.managesystem.ui.SearchOrderSaleCollectFragment.EXTRA_VLAUE";

  protected static final String EXTRA_STARTDATE =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_STARTDATE";
  protected static final String EXTRA_ENDDATE =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_ENDDATE";
  protected static final String EXTRA_DEPTNAME =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_DEPTNAME";
  protected static final String EXTRA_DEPTCODE =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_DEPTCODE";

  protected static final String EXTRA_CERTIFICATE_NAMES =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_CERTIFICATE_NAMES";
  protected static final String EXTRA_CERTIFICATE_CODES =
      "com.isunland.managesystem.ui.SearchDepatrtCollectFragment.EXTRA_CERTIFICATE_CODES";

  private static final int STARTDATE = 0;
  private static final int ENDDATE = 1;
  private static final int TYPE = 2;
  private static final int PERSON = 3;
  private static final int CUSTOMER = 4;
  private static final int IFALL = 5;

  private String type = "out";//出入库类型

  private String startDate = "";//开始时间
  private String endDate = "";//结束时间
  private String statisticType = "";
  private String statisticTypeName = "";
  private String manNameId = "";//业务员id
  private String manName = "";//业务员名字
  protected String customerName = "";//客户名字
  protected String customerId = "";//客户id
  private String ifBillAll = "";
  private String ifBillAllName = "全部";

  private OrderCollectContent collectContent;

  private TextView tvStartDate;
  private TextView tvEndDate;
  private TextView tvType;
  private TextView tvPerson;
  private TextView tvCustomerTitle;//标题
  private TextView tvCustomer;
  private TextView tvIfAll;
  private TextView tvIfAllTitle;

  private ImageView ibStartDate;
  private ImageView ibEndDate;
  private ImageView ibType;
  private ImageView ibPerson;
  private ImageView ibCustomer;
  private ImageView ibIfAll;

  @Override public void onCreate(Bundle savedInstanceState) {

    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.query);

    collectContent = (OrderCollectContent) getArguments().getSerializable(EXTRA_VLAUE);
    getData(collectContent);

    super.onCreate(savedInstanceState);
  }

  private void getData(OrderCollectContent content) {
    startDate = content.getStartDate();//开始时间
    endDate = content.getEndDate();//结束时间
    statisticType = content.getStatisticType();
    statisticTypeName = content.getStatisticTypeName();
    manNameId = content.getManNameId();
    manName = content.getManName();
    customerName = content.getCustomerName();//客户名字
    customerId = content.getCustomerId();//客户id
    ifBillAll = content.getIfBillAll();
    ifBillAllName = content.getIfBillAllName();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:
        vollelPostConfirm();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //回传 所选值
  private void vollelPostConfirm() {
    Intent backIntent = new Intent();
    OrderCollectContent collectContent =
        new OrderCollectContent(startDate, endDate, statisticType, statisticTypeName, manNameId,
            manName, customerId, customerName, ifBillAll, ifBillAllName);
    backIntent.putExtra(OrderSaleCollectFragment.EXTRA_VLAUE, collectContent);
    getActivity().setResult(Activity.RESULT_OK, backIntent);
    getActivity().finish();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_search_order_collect, container, false);
    findView(v);
    return v;
  }

  private void findView(View v) {
    tvPerson = (TextView) v.findViewById(R.id.tv_person);
    tvStartDate = (TextView) v.findViewById(R.id.tv_start_date);
    tvEndDate = (TextView) v.findViewById(R.id.tv_end_date);
    tvIfAll = (TextView) v.findViewById(R.id.tv_ifAll);
    tvCustomer = (TextView) v.findViewById(R.id.tv_customer);
    tvType = (TextView) v.findViewById(R.id.tv_type);
    tvIfAllTitle = (TextView) v.findViewById(R.id.tv_ifAll_title);
    tvCustomerTitle = (TextView) v.findViewById(R.id.tv_customer_title);

    ibPerson = (ImageView) v.findViewById(R.id.ib_person);
    ibStartDate = (ImageView) v.findViewById(R.id.ib_start_date);
    ibEndDate = (ImageView) v.findViewById(R.id.ib_end_date);
    ibType = (ImageView) v.findViewById(R.id.ib_type);
    ibIfAll = (ImageView) v.findViewById(R.id.ib_ifAll);
    ibCustomer = (ImageView) v.findViewById(R.id.ib_customer);

    ibPerson.setOnClickListener(this);
    ibStartDate.setOnClickListener(this);
    ibEndDate.setOnClickListener(this);
    ibType.setOnClickListener(this);
    ibIfAll.setOnClickListener(this);
    ibCustomer.setOnClickListener(this);
    //初始化数据
    tvStartDate.setText(startDate);
    tvEndDate.setText(endDate);
    tvType.setText(statisticTypeName);
    tvPerson.setText(manName);
    // ’01’代表出库单,’02’代表入库单
    if ("01".equals(statisticType)) {
      tvCustomerTitle.setText(R.string.customer_dept_colon);
      tvIfAllTitle.setText(R.string.if_all_out);
    } else if ("02".equals(statisticType)) {
      tvCustomerTitle.setText(R.string.supplier_dept_colon);
      tvIfAllTitle.setText(R.string.if_all_in);
    }
    tvCustomer.setText(customerName);
    tvIfAll.setText(ifBillAllName);

    //根据选择的出入库类型，显示字段不同
    tvType.addTextChangedListener(new TextWatcher() {

      @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        if ("入库".equals(tvType.getText().toString())) {
          tvCustomerTitle.setText(R.string.supplier_dept_colon);
          tvIfAllTitle.setText(R.string.if_all_in);
        } else if ("出库".equals(tvType.getText().toString())) {
          tvCustomerTitle.setText(R.string.customer_dept_colon);
          tvIfAllTitle.setText(R.string.if_all_out);
        }
        tvCustomer.setText("");
      }

      @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Auto-generated method stub

      }

      @Override public void afterTextChanged(Editable s) {
        // Auto-generated method stub

      }
    });
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.ib_start_date:
        showDialog(STARTDATE);
        break;

      case R.id.ib_end_date:
        showDialog(ENDDATE);
        break;
      case R.id.ib_type:
        showDialog(TYPE);
        //收入支出类型，可多选
        //			Intent personIntent=new Intent(getActivity(),TypeListActivity.class);
        //			startActivityForResult(personIntent, TYPE);

        break;
      case R.id.ib_person:
        //			showDialog(PERSON);
        Intent i = new Intent(getActivity(), AppointerListActivity.class);
        startActivityForResult(i, PERSON);
        break;
      //根据出库或入库选择不同供应商或部门、客户
      case R.id.ib_customer:
        Intent customerIntent = new Intent(getActivity(), CustomerChoosePagerActivity.class);
        customerIntent.putExtra(CustomerChoosePagerActivity.EXTRA_TYPE, type);//in:入库  out:出库
        startActivityForResult(customerIntent, CUSTOMER);
        break;
      case R.id.ib_ifAll:
        showDialog(IFALL);
        break;
      default:
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {
      //开始时间
      case STARTDATE:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      //结束时间
      case ENDDATE:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      //选择出库入库
      case TYPE:
        dialog = new ChooseTypeDialogFragment();
        break;
      //是否全数出库,入库
      case IFALL:
        dialog = new ChooseIfAllDialogFragment();
        break;
      //客户
      case CUSTOMER:
        dialog = new CustomerNameDialogFragment();
        break;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(SearchOrderSaleCollectFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {

      return;
    }
    //单据开始日期
    if (requestCode == STARTDATE) {
      Date mdate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      startDate = MyDateUtil.dateToString(mdate, MyDateUtil.TYPE_DATE);
      tvStartDate.setText(startDate);
      return;
    }
    //单据结束日期
    if (requestCode == ENDDATE) {
      Date startDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      endDate = MyDateUtil.dateToString(startDate, MyDateUtil.TYPE_DATE);
      tvEndDate.setText(endDate);
      return;
    }
    //业务员
    if (requestCode == PERSON) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      manName = customer.getName();
      manNameId = customer.getId();
      tvPerson.setText(manName);
    }
    //客户
    if (requestCode == CUSTOMER) {

      EnterpriseList item =
          (EnterpriseList) data.getSerializableExtra(EnterpriseListFragment.EXTRA_ENTERPRISE);
      customerName = item.getEnterpriseName();
      customerId = item.getId();
      tvCustomer.setText(customerName);
    }
    //出入库类型
    if (requestCode == TYPE) {

      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(ChooseTypeDialogFragment.EXTRA_VALUE);
      statisticTypeName = customer.getName();
      statisticType = customer.getId();
      tvType.setText(statisticTypeName);
      if ("01".equals(statisticType)) {
        type = "out";
      } else if ("02".equals(statisticType)) {
        type = "in";
      }
    }
    //是否全书入库
    if (requestCode == IFALL) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(ChooseIfAllDialogFragment.EXTRA_VALUE);
      ifBillAllName = customer.getName();
      ifBillAll = customer.getId();
      tvIfAll.setText(ifBillAllName);//数据状态
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  public static SearchOrderSaleCollectFragment newInstence(OrderCollectContent collectContent) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VLAUE, collectContent);
    SearchOrderSaleCollectFragment mfragment = new SearchOrderSaleCollectFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
